import AntModal from '@/components/pt/dialog/AntModal'
import SelectRegion from '@/views/platform/components/SelectRegion'
import { getRegionMap, addRegionMap, updateRegionMap } from '@/api/platform/regionMap'
import TXMap from '@/components/pt/txmap/TXMap'
export default {
  name: 'CreateForm',
  props: {
    regionId: {
      type: String,
      required: false
    }
  },
  components: {
    AntModal,
    SelectRegion,
    TXMap
  },
  data() {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      selectDisable: false,
      disabled: false,
      inputDisable: true,
      total: 0,
      id: undefined,
      formTitle: '添加地区地图',
      mapHeight: '350px',
      // 表单参数
      form: { regionId: this.regionId },
      formClone: {},
      rules: {
        name: [{ required: true, message: '区域地图名称不能为空', trigger: 'blur' }],

        longitude: [{ required: true, message: '区域地图纬度不能为空', trigger: 'blur' }],

        latitude: [{ required: true, message: '区域地图经度不能为空', trigger: 'blur' }],

        range: [{ required: true, message: '区域地图范围半径不能为空', trigger: 'blur' }],

        scale: [{ required: true, message: '区域地图缩放等级不能为空', trigger: 'blur' }],

        longitudinalOffset: [{ required: true, message: '区域地图纵向偏移量不能为空', trigger: 'blur' }],

        lateralOffset: [{ required: true, message: '区域地图横向偏移量不能为空', trigger: 'blur' }],

        padding: [{ required: true, message: '区域地图文本边缘留白不能为空', trigger: 'blur' }],

        regionId: [{ required: true, message: '请选择地区', trigger: 'blur' }]
      }
    }
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,

        regionId: this.regionId,

        longitude: undefined,

        latitude: undefined,

        range: undefined,

        scale: undefined,

        longitudinalOffset: undefined,

        lateralOffset: undefined,

        padding: undefined
      }
    },
    /** 判断是否有地区 */
    judgeRegion() {
      if (this.regionId !== undefined) {
        this.selectDisable = true
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.judgeRegion()
      this.open = true
      this.formTitle = '添加地区地图'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.judgeRegion()
      this.open = true
      this.spinning = !this.spinning
      const regionMapId = row.id
      this.form.regionId = row.regionId
      getRegionMap(regionMapId).then(response => {
        this.form = response.data
        this.formTitle = '修改地区地图'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.id !== undefined) {
            updateRegionMap(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addRegionMap(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back() {
      const index = '/platform/regionmap/index'
      this.$router.push(index)
    }
  }
}
